package com.mrkun.vueshop.controller;

import com.mrkun.vueshop.DTO.PurchaseDTO;
import com.mrkun.vueshop.mapper.OrderMapper;
import com.mrkun.vueshop.mapper.UserMapper;
import com.mrkun.vueshop.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author by yong
 * @date 2020/3/21
 */
@RestController
public class PurchaseController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @PostMapping("/purchase")
    public String purchase(@RequestBody PurchaseDTO purchaseDTO){
        try{
            Order order = new Order();
            order.setFundId(purchaseDTO.getId());
            order.setFundName(purchaseDTO.getName());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            order.setCreateTime(sdf.format(new Date()));
            order.setMoney(purchaseDTO.getMoney());
            order.setState(1);
            order.setTotalIncome(new BigDecimal(0.00));
            order.setUserId(userMapper.getOneByName(purchaseDTO.getUsername()).getId());
            orderMapper.addOrder(order);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "failure";
        }
    }
}
